<!DOCTYPE html>
<html>
    <head>
        <title>Test multiple MediaKeys lifetimes</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            // For this test, create several MediaKeys and verify lifetime.
            promise_test(function(test)
            {
                var mediaKeys1;
                var mediaKeys2;
                var mediaKeys3;
                var mediaKeys4;
                var mediaKeys5;

                // Create a MediaKeys object. Returns a promise that resolves
                // with the new MediaKeys object.
                function createMediaKeys()
                {
                    return navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration()).then(function(access) {
                        return access.createMediaKeys();
                    });
                }

                // Create a few MediaKeys objects. Keep references to them,
                // to avoid issues if gc() runs.
                // Even though there should be no existing objects, start by
                // running gc() and verifying that none exist. This also
                // allows the failures to be reported from inside a promise
                // so that the test fails properly.
                return createGCPromise().then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(0, 0, 'After initial gc()');

                    return createMediaKeys();
                }).then(function(result) {
                    assert_not_equals(result, null);
                    mediaKeys1 = result;
                    verifyMediaKeyAndMediaKeySessionCount(1, 0, 'Create first MediaKeys');

                    return createMediaKeys();
                }).then(function(result) {
                    assert_not_equals(result, null);
                    mediaKeys2 = result;
                    verifyMediaKeyAndMediaKeySessionCount(2, 0, 'Create second MediaKeys');

                    return createMediaKeys();
                }).then(function(result) {
                    assert_not_equals(result, null);
                    mediaKeys3 = result;
                    verifyMediaKeyAndMediaKeySessionCount(3, 0, 'Create third MediaKeys');

                    return createMediaKeys();
                }).then(function(result) {
                    assert_not_equals(result, null);
                    mediaKeys4 = result;
                    verifyMediaKeyAndMediaKeySessionCount(4, 0, 'Create fourth MediaKeys');

                    return createMediaKeys();
                }).then(function(result) {
                    assert_not_equals(result, null);
                    mediaKeys5 = result;
                    verifyMediaKeyAndMediaKeySessionCount(5, 0, 'Create fifth MediaKeys');

                    // In order for the MediaKey objects to be garbage
                    // collected, it needs time to process any pending events.
                    return delayToAllowEventProcessingPromise();
                }).then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(5, 0, 'All alive');

                    // Now run garbage collection. Since we have references to
                    // all 5 MediaKeys, none will be collected.
                    return createGCPromise();
                }).then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(5, 0, 'All still alive');

                    // Drop references to 4 of the MediaKeys. As we will only
                    // have a reference to the last created MediaKeys object,
                    // the other 4 MediaKeys objects are available to be
                    // garbage collected.
                    mediaKeys1 = null;
                    mediaKeys2 = null;
                    mediaKeys3 = null;
                    mediaKeys4 = null;
                    return createGCPromise();
                }).then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(1, 0, 'Only 1 left');

                    // Release the last MediaKeys object created.
                    mediaKeys5 = null;

                    // Run gc() again to reclaim the remaining MediaKeys object.
                    return createGCPromise();
                }).then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(0, 0, 'After final gc()');
                });
            }, 'Multiple MediaKeys lifetime');
        </script>
    </body>
</html>
